/**
 * @file       EventHandler.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 12, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include <algorithm>
#include "EventHandler.h"
#include "typedefine.h"
#include "errorcode.h"
#include "IEventListener.h"
#include "IEvent.h"

namespace NS_LCD_AFW
{

CEventHandler::CEventHandler()
{
	m_eventListenerList.clear();
}

CEventHandler::~CEventHandler()
{
	release();
}

int CEventHandler::addEventListener(IEventListener *pEventListener)
{
	if (NULL != pEventListener)
	{
		m_eventListenerList.push_back(pEventListener);
		pEventListener->setOwner(this);
		return ERR_SUCCESS;
	}
	return ERR_INVALID_LISTENER;
}

int CEventHandler::removeEventListener(IEventListener *&pEventListener)
{
	IEventListenerList::iterator iter = m_eventListenerList.end();
	if (NULL != pEventListener
			&& ((iter = std::find(m_eventListenerList.begin(),
					m_eventListenerList.end(), pEventListener))
					!= m_eventListenerList.end()))
	{
		(*iter)->destory();
		m_eventListenerList.erase(iter);
		pEventListener = NULL;
		return ERR_SUCCESS;
	}
	return ERR_INVALID_LISTENER;
}

int CEventHandler::notify(IEvent *pEvent)
{
	IEventListenerList::reverse_iterator iter = m_eventListenerList.rbegin();
	if (iter == m_eventListenerList.rend())
	{
		return ERR_NO_EVENT_LISTENER;
	}
	for (; iter != m_eventListenerList.rend(); ++iter)
	{
		if (ERR_SUCCESS == (*iter)->actionPerformed(pEvent))
		{
			return ERR_SUCCESS;
		}
	}
	return ERR_EVENT_UN_HANDLED;
}

int CEventHandler::broadcast(IEvent *pEvent)
{
	IEventListenerList::reverse_iterator iter = m_eventListenerList.rbegin();
	if (iter == m_eventListenerList.rend())
	{
		return ERR_NO_EVENT_LISTENER;
	}
	for (; iter != m_eventListenerList.rend(); ++iter)
	{
		(*iter)->actionPerformed(pEvent);
	}
	return ERR_SUCCESS;
}

int CEventHandler::handler(IEvent *pEvent)
{
	if (NULL == pEvent)
	{
		return ERR_INVALID_EVENT;
	}

	int iRet = ERR_UNKNOW_EVENT;
	switch (pEvent->getType())
	{
	case IEvent::BROADCAST_EVENT:
	{
		//iRet = this->broadcast(pEvent);
	}
		break;
	case IEvent::NOTIFY_EVENT:
	{
		iRet = this->notify(pEvent);
	}
		break;
	default:
		break;
	}
	return iRet;
}

int CEventHandler::handlerEvent(IEvent *pEvent)
{
	if (NULL == pEvent)
	{
		return ERR_INVALID_EVENT;
	}

	return ERR_EVENT_UN_HANDLED;
}

AFW_EventCallBack CEventHandler::getCallBack(int eventId) const
{
	AFW_EventCallBack pRet = NULL;
	IEventMap::const_iterator iter = m_eventCallBackMap.find(eventId);
	if (iter != m_eventCallBackMap.end())
	{
		pRet = iter->second;
	}
	return pRet;
}

void CEventHandler::registerCallBack(int eventId, AFW_EventCallBack pCallBack)
{
	if (NULL != pCallBack)
	{
		m_eventCallBackMap[eventId] = pCallBack;
	}
}

void CEventHandler::release()
{
	IEventListenerList::reverse_iterator iter = m_eventListenerList.rbegin();
	for (; iter != m_eventListenerList.rend(); ++iter)
	{
		(*iter)->destory();
		(*iter) = NULL;
	}
	m_eventListenerList.clear();
}

} /* namespace NS_LCD_AFW */
